import SettingPanel, { SettingPanelType } from "./SettingPanel";
import GameManager, { GameType } from "../Manager/GameManager";
import TipPanel from "./TipPanel";
import UIManager from "../Manager/UIManager";
import WXSubContextViewPanel from "./WXSubContextViewPanel";
import GamePlayPanel from "./GamePlayPanel";


const { ccclass, property } = cc._decorator;

@ccclass
export default class StartPanel extends cc.Component {
    public static Instance: StartPanel;

    @property(cc.Button)
    Button_normal: cc.Button = null;
    @property(cc.Button)
    Button_speed: cc.Button = null;

    @property(cc.Button)
    Button_setting: cc.Button = null;
    @property(cc.Button)
    Button_rank: cc.Button = null;
    @property(cc.Button)
    Button_rank_close: cc.Button = null;

    @property(cc.Button)
    Button_rank_pre: cc.Button = null;
    @property(cc.Button)
    Button_rank_next: cc.Button = null;

    @property(cc.Button)
    Button_1: cc.Button = null;
    @property(cc.Button)
    Button_2: cc.Button = null;


    @property(cc.Slider)
    slider_x: cc.Slider = null;
    @property(cc.Slider)
    slider_y: cc.Slider = null;
    


    @property(cc.Label)
    scoreDisplay: cc.Label = null;
    


    x_prev_tag = false;
    y_prev_tag = false;
    x_prev_val = false;
    y_prev_val = false;
    score = 0;

    onLoad() {
        StartPanel.Instance = this;
    }



    //自定义回调函数,参数t
    on_touch_move(t) {
        //定义一个n_pos变量存储当前触摸点的位置
        var n_pos = t.getLocation();
        //打印触摸点的坐标，x坐标，y坐标
        console.log(n_pos, n_pos.x, n_pos.y);

        //定义变量delta存储变化距离
        var delta = t.getDelta();
        //变化当前节点位置使其跟随触摸点,实现按住移动效果
        this.Button_1.node.x += delta.x;
        this.Button_1.node.y += delta.y;
    }


    _isGetScore3(nowGX,nowGY){
        var stand_x = this.slider_x.progress; //0.25;
        var stand_y = this.slider_x.progress; //0.25;
        if(this.x_prev_tag){  //前一次为 正 
            stand_x = -stand_x;
 
            if(nowGX <= stand_x){
                var cur_x_tag = false;
            }else{
                var cur_x_tag = true;
            }
        }else {    //前一次为负
            if(nowGX >= stand_x){
                var cur_x_tag = true;
            }else{
                var cur_x_tag = false;
            }
        }
 
        if(this.y_prev_tag){  //前一次为 正 
            stand_y = -stand_y;
 
            if(nowGY <= stand_y){
                var cur_y_tag = false;
            }else{
                var cur_y_tag = true;
            }
        }else {    //前一次为负
            if(nowGY >= stand_y){
                var cur_y_tag = true;
            }else{
                var cur_y_tag = false;
            }
        }
 
 
        var x_is = false;
        var y_is = false;
        if(( this.x_prev_tag == true && cur_x_tag == false) ||
            (this.x_prev_tag == false && cur_x_tag == true)
        ){
 
            //console.log('x prev:cur==',this.x_prev_val, '##',nowGX);
            this.x_prev_val = nowGX;
            this.x_prev_tag = cur_x_tag;
            x_is = true;
        }
 
        if((this.y_prev_tag == true && cur_y_tag == false) ||
            (this.y_prev_tag == false && cur_y_tag == true)
        ){
            //console.log('y prev:cur==', this.y_prev_val, '##',nowGY)
            this.y_prev_val = nowGY;
            this.y_prev_tag = cur_y_tag;
            y_is = true;
        }
 
        return x_is || y_is;
    }
    

    //x,y,z方向的重力感应加速度
    onDeviceMotionEvent(event) {
        console.log("event name:", event.type, " acc x:", event.acc.x, " acc y:", event.acc.y, " acc z:", event.acc.z); //单位是g=9.8m/s^2

        var nowGX = event.acc.x.toFixed(2);
        var nowGY = event.acc.y.toFixed(2);
        var nowGZ = event.acc.z.toFixed(2);
 
 
        if(this._isGetScore3(nowGX,nowGY)){
            this.score += 1;
            this.scoreDisplay.string = this.score.toString();
        }
    }


    start() {
        this.scoreDisplay.string = this.score.toString();

        cc.systemEvent.setAccelerometerEnabled(true);
        cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this); //注册重力传感响应事件


        //监听触摸开始事件
        this.Button_1.node.on(cc.Node.EventType.TOUCH_START, function (t) {
            //函数体内写事件发生时的事情
            //当触摸开始是打印以下字样
            console.log("触摸开始");
        }, this);

        //监听触摸移动事件
        //使用自定义回调函数
        this.Button_1.node.on(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move, this);
        //结束触摸移动事件
        //this.node.off(cc.Node.EventType.TOUCH_MOVE,this.on_touch_move,this);

        //监听作用域内触摸抬起事件
        this.Button_1.node.on(cc.Node.EventType.TOUCH_END, function (t) {
            console.log("触摸内结束");
        }, this);
        //监听作用域外触摸抬起事件
        this.Button_1.node.on(cc.Node.EventType.TOUCH_CANCEL, function (t) {
            console.log("触摸外开始");
        }, this);


        //监听触摸开始事件
        this.Button_2.node.on(cc.Node.EventType.TOUCH_START, function (t) {
            //函数体内写事件发生时的事情
            //当触摸开始是打印以下字样
            console.log("触摸开始");
        }, this);

        //监听触摸移动事件
        //使用自定义回调函数
        this.Button_2.node.on(cc.Node.EventType.TOUCH_MOVE, function (t) {
            //定义一个n_pos变量存储当前触摸点的位置
            var n_pos = t.getLocation();
            //打印触摸点的坐标，x坐标，y坐标
            console.log(n_pos, n_pos.x, n_pos.y);

            //定义变量delta存储变化距离
            var delta = t.getDelta();
            //变化当前节点位置使其跟随触摸点,实现按住移动效果
            this.Button_2.node.x += delta.x;
            this.Button_2.node.y += delta.y;
        }, this);

        //结束触摸移动事件
        //this.node.off(cc.Node.EventType.TOUCH_MOVE,this.on_touch_move,this);

        //监听作用域内触摸抬起事件
        this.Button_2.node.on(cc.Node.EventType.TOUCH_END, function (t) {
            console.log("触摸内结束");
        }, this);
        //监听作用域外触摸抬起事件
        this.Button_2.node.on(cc.Node.EventType.TOUCH_CANCEL, function (t) {
            console.log("触摸外开始");
        }, this);




        // 普通模式
        this.Button_normal.node.on('click', (button) => {
            StartPanel.Instance.ShowOff();
            // GamePlayPanel.Instance.ShowOn();
            GameManager.Instance.gameType = GameType.Normal;
            GameManager.Instance.GameStart();
        }, this);

        // 竞速模式
        this.Button_speed.node.on('click', (button) => {
            StartPanel.Instance.ShowOff();
            // GamePlayPanel.Instance.ShowOn();
            GameManager.Instance.gameType = GameType.Speed;
            GameManager.Instance.GameStart();
        }, this);

        // 设置界面
        this.Button_setting.node.on('click', (button) => {
            console.log(cc.sys.platform);
            console.log(cc.sys.WECHAT_GAME);
            console.log(cc.sys.isNative);

            SettingPanel.Instance.ShowOn(SettingPanelType.setting);
        }, this);

        // 打开排行榜
        this.Button_rank.node.on('click', () => {
            // TipPanel.Instance.Show("排行榜");
            // UIManager.Instance.wxSubContextView.node.active = true;

            // if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            //     WXSubContextViewPanel.Instance.rankControl.active = true;
            //     wx.getOpenDataContext().postMessage({
            //         msg: 'openRank',
            //         userInfo: GameManager.Instance.userInfo,
            //     });
            // }
        }, this);
    }

    ShowOn() {
        this.node.active = true;
    }

    ShowOff() {
        this.node.active = false;
    }

}